import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let list = JSON.parse(localStorage.getItem("shuju"));
function mppare(msy) {
  return function (a, b) {
    var value1 = a[msy];
    var value2 = b[msy];
    return value1 - value2
  }
}
function mppare1(msy) {
  return function (a, b) {
    var value1 = a[msy];
    var value2 = b[msy];
    return   value2-value1
  }
}
export default new Vuex.Store({
  state: {
    list: list ? list : [],
    lis:[]
  },
  actions: {

  },
  mutations: {
    add(state) {
      state.list.sort(mppare("money"))
    },add1(state) {
      state.list.sort(mppare("num"))
    },add2(state) {
      state.list.sort(mppare1("money"))
    },add3(state) {
      state.list.sort(mppare1("num"))
    },
    addlis(state,id){
      var obj=state.list.find(item=>{
        return item.id===id
      })
      var index=state.lis.findIndex(item=>{
        return item.id===obj.id
      })
      if(index===-1){
        state.lis.push(obj);
      }else{
        state.lis.find(item=>{
          if(item.id===obj.id){
            item.count++
          }
        })
      }
      console.log(state.lis);
    },
    jia(state,id){
      state.lis.find(item=>{
        if(item.id===id){
          item.count++
        }
      })
    },
    jian(state,id){
      state.lis.find(item=>{
        if(item.id===id){
          if(item.count>1){
             item.count--
          }else{
           var num= state.lis.findIndex(item=>{
              return item.id===id
            })
            state.lis.splice(num,1)
          }
        }
      })
    }
  },
  modules: {
  },
  getters:{
    sum(state){
      return state.lis
    }
  }
})
